<?php

namespace App\Models\V1;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

/**
 * @method static enable()
 * @method static get()
 * @method static findOrFail(int $id)
 * @method static where(string $string, $id)
 * @property int|mixed parent_id
 * @property mixed|string name
 * @property mixed|string title
 * @property mixed|string icon
 * @property mixed|string path
 * @property mixed|string component_path
 * @property int|mixed is_enable
 */
class Menu extends Model
{
    protected $table = 'menus';

    const ENABLE_TRUE  = 1; // 开启
    const ENABLE_FALSE = 0; // 禁止

    const CREATED_AT = 'created_time';
    const UPDATED_AT = 'updated_time';

    /**
     * A permission can be applied to roles.
     */
    public function roles(): BelongsToMany
    {
        return $this->belongsToMany(
            Role::class,
            config('permission.table_names.role_has_menus'),
            'menu_id',
            'role_id'
        );
    }


    /**
     * 是否有效
     * @param Builder $query
     * @return Builder
     */
    public function scopeEnable(Builder $query): Builder
    {
        return $query->where('is_enable', self::ENABLE_TRUE);
    }
}

